package com.yunkeji.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.utils.StringUtils;
import com.yunkeji.dao.domain.AppUserExt;
import com.yunkeji.dao.domain.SysCity;
import com.yunkeji.dao.mapper.AppUserExtMapper;
import com.yunkeji.dao.mapper.SysCityMapper;
import com.yunkeji.dao.vo.AppUserExtVo;
import com.yunkeji.system.domain.bo.AppUserExtBo;
import com.yunkeji.system.service.IAppUserExtService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 用户基本信息Service业务层处理
 *
 * @author zhang
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class AppUserExtServiceImpl implements IAppUserExtService {

    private final AppUserExtMapper baseMapper;
    private final SysCityMapper sysCityMapper;
    /** 查询用户基本信息 */
    @Override
    public AppUserExtVo queryById(Long id) {
        AppUserExtVo appUserExtVo = baseMapper.selectVoById(id);
        if (appUserExtVo.getLiveCity() != null) {
            SysCity city = sysCityMapper.selectById(appUserExtVo.getLiveCity());
            if (city != null) {
                appUserExtVo.setLiveCityName(city.getName());
            }
            SysCity province = sysCityMapper.selectById(appUserExtVo.getLiveProvince());
            if (province != null) {
                appUserExtVo.setLiveProvinceName(province.getName());
            }
        }
        return appUserExtVo;
    }

    /** 查询用户基本信息列表 */
    @Override
    public TableDataInfo<AppUserExtVo> queryPageList(AppUserExtBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppUserExt> lqw = buildQueryWrapper(bo);
        Page<AppUserExtVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /** 查询用户基本信息列表 */
    @Override
    public List<AppUserExtVo> queryList(AppUserExtBo bo) {
        LambdaQueryWrapper<AppUserExt> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppUserExt> buildQueryWrapper(AppUserExtBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppUserExt> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getAge() != null, AppUserExt::getAge, bo.getAge());
        lqw.eq(bo.getHeight() != null, AppUserExt::getHeight, bo.getHeight());
        lqw.eq(StringUtils.isNotBlank(bo.getHeadUrl()), AppUserExt::getHeadUrl, bo.getHeadUrl());
        lqw.eq(bo.getBirthDate() != null, AppUserExt::getBirthDate, bo.getBirthDate());
        lqw.eq(bo.getEducation() != null, AppUserExt::getEducation, bo.getEducation());
        lqw.eq(bo.getProfession() != null, AppUserExt::getProfession, bo.getProfession());
        lqw.eq(bo.getConstellation() != null, AppUserExt::getConstellation, bo.getConstellation());
        lqw.eq(bo.getSentiment() != null, AppUserExt::getSentiment, bo.getSentiment());
        lqw.eq(bo.getTryst() != null, AppUserExt::getTryst, bo.getTryst());
        lqw.eq(bo.getLiveTogether() != null, AppUserExt::getLiveTogether, bo.getLiveTogether());
        lqw.eq(bo.getMood() != null, AppUserExt::getMood, bo.getMood());
        lqw.eq(bo.getLiveCity() != null, AppUserExt::getLiveCity, bo.getLiveCity());
        lqw.eq(bo.getLiveProvince() != null, AppUserExt::getLiveProvince, bo.getLiveProvince());
        lqw.eq(StringUtils.isNotBlank(bo.getHobby()), AppUserExt::getHobby, bo.getHobby());
        lqw.eq(
                StringUtils.isNotBlank(bo.getMomentsPics()),
                AppUserExt::getMomentsPics,
                bo.getMomentsPics());
        lqw.eq(
                StringUtils.isNotBlank(bo.getVoiceIntro()),
                AppUserExt::getVoiceIntro,
                bo.getVoiceIntro());
        lqw.eq(
                StringUtils.isNotBlank(bo.getVideoIntro()),
                AppUserExt::getVideoIntro,
                bo.getVideoIntro());
        lqw.eq(
                StringUtils.isNotBlank(bo.getAlbumPics()),
                AppUserExt::getAlbumPics,
                bo.getAlbumPics());
        lqw.eq(
                bo.getLatestCallDate() != null,
                AppUserExt::getLatestCallDate,
                bo.getLatestCallDate());
        lqw.eq(bo.getRegDate() != null, AppUserExt::getRegDate, bo.getRegDate());
        lqw.eq(bo.getCallScore() != null, AppUserExt::getCallScore, bo.getCallScore());
        lqw.eq(bo.getCallCount() != null, AppUserExt::getCallCount, bo.getCallCount());
        lqw.eq(StringUtils.isNotBlank(bo.getLoginIp()), AppUserExt::getLoginIp, bo.getLoginIp());
        lqw.eq(bo.getLoginDate() != null, AppUserExt::getLoginDate, bo.getLoginDate());
        lqw.eq(StringUtils.isNotBlank(bo.getCardNum()), AppUserExt::getCardNum, bo.getCardNum());
        lqw.like(
                StringUtils.isNotBlank(bo.getRealName()),
                AppUserExt::getRealName,
                bo.getRealName());
        lqw.eq(bo.getCallSuccess() != null, AppUserExt::getCallSuccess, bo.getCallSuccess());
        lqw.eq(bo.getCallFail() != null, AppUserExt::getCallFail, bo.getCallFail());
        lqw.eq(bo.getFreeMsgCount() != null, AppUserExt::getFreeMsgCount, bo.getFreeMsgCount());
        lqw.eq(
                bo.getCallDurationSum() != null,
                AppUserExt::getCallDurationSum,
                bo.getCallDurationSum());
        lqw.eq(bo.getWeight() != null, AppUserExt::getWeight, bo.getWeight());
        lqw.eq(bo.getIncome() != null, AppUserExt::getIncome, bo.getIncome());
        return lqw;
    }

    /** 新增用户基本信息 */
    @Override
    public Boolean insertByBo(AppUserExtBo bo) {
        AppUserExt add = BeanUtil.toBean(bo, AppUserExt.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /** 修改用户基本信息 */
    @Override
    public Boolean updateByBo(AppUserExtBo bo) {
        AppUserExt update = BeanUtil.toBean(bo, AppUserExt.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /** 保存前的数据校验 */
    private void validEntityBeforeSave(AppUserExt entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /** 批量删除用户基本信息 */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean deleteHeadUrl(Long id) {
        baseMapper.updateById(new AppUserExt().setHeadUrl("").setId(id));
        return true;
    }

    @Override
    public Boolean deleteVideo(Long id) {
        baseMapper.updateById(new AppUserExt()
            .setVideoIntro("")
            .setVideoIntroCover("")
            .setVideoIntroCoverGif("")
            .setVideoIntroTime(0)
            .setId(id));
        return true;
    }
}
